# - Find the LLVM includes and library
# This module defines
#  LLVM_INCLUDE_DIRS: Where to find LLVM headers
#  LLVM_CXX_FLAGS: C++ flags required to compile source using LLVM
#  LLVM_LIBRARIES: The libraries to link against to use LLVM
#  LLVM_FOUND: LLVM has been found

find_program(LLVM_CONFIG llvm-config)
if(LLVM_CONFIG)
  execute_process(
    COMMAND ${LLVM_CONFIG} --cxxflags
    OUTPUT_VARIABLE LLVM_CXXFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  execute_process(
    COMMAND ${LLVM_CONFIG} --includedir
    OUTPUT_VARIABLE LLVM_INCLUDE_DIR
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  execute_process(
    COMMAND ${LLVM_CONFIG} --ldflags
    OUTPUT_VARIABLE LLVM_LDFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
  execute_process(
    COMMAND ${LLVM_CONFIG} --libfiles bitwriter linker ipo instrumentation backend jit
    OUTPUT_VARIABLE LLVM_LIBRARY
    OUTPUT_STRIP_TRAILING_WHITESPACE
  )
endif()

if(LLVM_CXXFLAGS AND LLVM_INCLUDE_DIR AND LLVM_LDFLAGS AND LLVM_LIBRARY)
  set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIR})
  set(LLVM_LIBRARIES ${LLVM_LIBRARY} ${LLVM_LDFLAGS})
  set(LLVM_FOUND YES)
else()
  set(LLVM_FOUND NO)
endif()

if(LLVM_FOUND)
  if(NOT LLVM_FIND_QUIETLY)
    message(STATUS "Found LLVM!")
  endif()
else()
  if(LLVM_FIND_REQUIRED)
    message(FATAL_ERROR "Could not find LLVM library")
  endif()
endif()

mark_as_advanced(LLVM_CONFIG LLVM_INCLUDE_DIR LLVM_LIBRARY LLVM_LDFLAGS)
